package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private DishServiceImpl dishService;


    @Autowired
    private SetmealServiceImpl setmealService;



    public int delete(Long id){
        //检查当前分类是否有以下菜品
        LambdaQueryWrapper<Dish> lqw = Wrappers.lambdaQuery(Dish.class).eq(Dish::getCategoryId, id);
        if (dishService.count(lqw) != 0) {
            //已经关联菜品，抛出一个业务异常
            return 0;

        }
        //检查当前分类是否有以下套餐
        LambdaQueryWrapper<Setmeal> lqw2 = Wrappers.lambdaQuery(Setmeal.class).eq(Setmeal::getCategoryId, id);
        if (setmealService.count(lqw2) != 0) {
            //已经关联套餐，抛出一个业务异常
            return 1;
        }

        //可以删除
        removeById(id);
        log.info("删除成功");
        return 2;

    }
}
